// 初始化属性
let timer = null;
var mapDataRender = [];
var timeDate = "map" + new Date().getTime();
var mapGeoJson;
var center = {
  南京市: [118.767413, 32.041544],
  无锡市: [120.301663, 31.574729],
  徐州市: [117.184811, 34.261792],
  常州市: [119.946973, 31.772752],
  苏州市: [120.619585, 31.299379],
  南通市: [120.864608, 32.016212],
  连云港市: [119.178821, 34.600018],
  淮安市: [119.021265, 33.597506],
  盐城市: [120.139998, 33.377631],
  扬州市: [119.421003, 32.393159],
  镇江市: [119.452753, 32.204402],
  泰州市: [119.915176, 32.484882],
  宿迁市: [118.275162, 33.963008],
};


async function updateMap(data, VueInstance) {
  let mapGeoJsonData = data;
  echarts.registerMap(VueInstance.options.geo[0].map, mapGeoJsonData);
  mapDataRender = mapGeoJsonData.features.map((item) => {
    return {
      value: (Math.random() * 1000).toFixed(2),
      name: item.properties.name,
    };
  });
  if (VueInstance) {
    VueInstance.options.series[0].data = mapDataRender;
    VueInstance.updateOptions(VueInstance.options);
  }
}

// 组件创建前
async function defineComponentBeforeCreated({ VueElement }) {
  console.log("defineComponentBeforeCreated");
  debugger
  try {
    if (VueElement) {
      // 兼容2.5的写法，已经无全局变量需要调整处理
      if (
        VueElement.component.props &&
        VueElement.component.props.mapInfo.json
      ) {
        // 刷新后读取最新的数据
        window[`${VueElement.id}mapGepJson`] =
          VueElement.component.props.mapInfo.json;
      } else {
        // 若无切换图层则使用默认的数据
        let mapUrl =
          BASE_URL +
          "/adminapp/public/assets/elfinder/files/json/geo/广州.json";
        let getMapJson = function () {
          return fetch(mapUrl).then((res) => res.json());
        };
        window[`${VueElement.id}mapGepJson`] = await getMapJson();
      }
      echarts.registerMap(VueElement.id, window[`${VueElement.id}mapGepJson`]);
    } else {
      // 兼容在组件库展示效果的写法
      let mapUrl =
        BASE_URL + "/adminapp/public/assets/elfinder/files/json/geo/广州.json";
      let getMapJson = function () {
        return fetch(mapUrl).then((res) => res.json());
      };
      mapGeoJson = await getMapJson();
      echarts.registerMap(VueElement ? VueElement.id : timeDate, mapGeoJson);
    }
  } catch (error) {
    console.log("error1", error);
  }
}

// 组件创建后
async function defineComponentCreated({ VueInstance, VueElement }) {
  console.log("defineComponentCreated");
  debugger
  try {
    if (!window[`${VueElement.id}mapGepJson`]) return
    if (VueElement && VueInstance) {
      newMapDate = VueElement.id;
      if (newMapDate) {
        VueInstance.options.geo[0].map = newMapDate;
        VueInstance.options.series[3].mapType = newMapDate;
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`]);
      } else {
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`]);
      }

      const myChart = VueInstance.getEChartInstance();
      timer = setInterval(() => {
        if (!center) return
        //数据情况重绘，清除formatter移动效果，也可根据自身需求是否需要，删除这两行代码
        /*option.series[seriesjson[runidx].createType-1].data = [];
          myChart.setOption(option, true);*/
        for (var i = 0; i < 3; i++) {
          VueInstance.options.series[i].data = [];
        }
        var cityIndex = Math.floor(Math.random() * 13);
        var runidx = Math.floor(Math.random() * 3);
        var coordCity = Object.keys(center)[cityIndex];
        var coord = center[coordCity];
        VueInstance.options.series[runidx].data = [
          {
            city: coordCity,
            value: coord,
          },
        ];
        // console.log(option.series[runidx].data)
        // console.log("---",VueInstance.options.series[4].data)
        // VueInstance.options.series[4].data = VueInstance.options.series[4].data.sort(function(a,b){
        //     return b.value-a.value;
        // });
        myChart.setOption(VueInstance.options, true);
      }, Math.floor(Math.random() * 10000) + 1000);
    }
  } catch (error) {
    console.log("error2", error);
  }
}

async function handleDataCallback({ ResourceData, VueInstance }) {
  console.log("handleDataCallback");
  debugger
  VueInstance && updateMap(ResourceData, VueInstance);
}
